/* $Id: ContestProblemScoreEntry.java 134 2011-08-27 16:48:38Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.datatypes.scores;

import java.io.Serializable;
import java.util.Date;

import javax.cache.Cache;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import alt.djudge.frontend.server.datatypes.SubmissionEntry;
import alt.djudge.frontend.server.models.CacheModel;


@PersistenceCapable(detachable="true")
@Inheritance(customStrategy = "complete-table")
public class ContestProblemScoreEntry extends AbstractScoreEntry implements Serializable, ContestScoreInterface, ProblemScoreInterface
{
	private static final long serialVersionUID = 1L;
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	protected Long id;
	
	@Override
	public Long getId()
	{
		return id;
	}
	
	@Override
	public void setId(Long id)
	{
		this.id = id;
	}
	
	@Persistent
	protected Long contestId;

	@Persistent
	protected Long contestProblemId;
	
	@Persistent
	protected Integer maxScore = -1;
	
	@Persistent
	protected Date maxScoreFirstDate;
	
	@Persistent
	protected Long maxScoreFirstTime = -1L;
	
	@Persistent
	protected Long maxScoreId;
	
	public void setContestProblemId(Long contestProblemId)
	{
		this.contestProblemId = contestProblemId;
	}

	public Long getContestProblemId()
	{
		return contestProblemId;
	}

	public void setMaxScore(Integer maxScore)
	{
		this.maxScore = maxScore;
	}

	public Integer getMaxScore()
	{
		return maxScore;
	}

	public void setMaxScoreFirstDate(Date maxScoreFirstDate)
	{
		this.maxScoreFirstDate = maxScoreFirstDate;
	}

	public Date getMaxScoreFirstDate()
	{
		return maxScoreFirstDate;
	}

	public void setMaxScoreFirstTime(Long maxScoreFirstTime)
	{
		this.maxScoreFirstTime = maxScoreFirstTime;
	}

	public Long getMaxScoreFirstTime()
	{
		return maxScoreFirstTime;
	}

	public void setMaxScoreId(Long maxScoreId)
	{
		this.maxScoreId = maxScoreId;
	}

	public Long getMaxScoreId()
	{
		return maxScoreId;
	}
	
	public Boolean getAccepted()
	{
		return uniqueAcceptedCount > 0;
	}
	
	@Override
	public void prepareForRejudge()
	{
		super.prepareForRejudge();
		maxScore = -1;
		maxScoreFirstDate = null;
		maxScoreFirstTime = -1L;
		maxScoreId = null;
	}
	
	public ContestProblemScoreEntry()
	{
		// TODO Auto-generated constructor stub
	}
	
	public ContestProblemScoreEntry(Long contestId,  Long contestProblemId)
	{
		setContestProblemId(contestProblemId);
		setContestId(contestId);
	}

	@Override
	public Long getContestId()
	{
		return contestId;
	}

	@Override
	public void setContestId(Long contestId)
	{
		this.contestId = contestId;
	}
	
	@Override
	public void clearCacheCustom()
	{
		Cache cache = CacheModel.getStaticCache();
		String keyContestProblem = CacheModel.getContestProblemScoreEntryKey(contestId, contestProblemId);
		cache.remove(keyContestProblem);
		//String keyProblem = CacheModel.getProblemScoreEntryKey(contestProblemId);
		//cache.remove(keyProblem);
	}
	
	@Override
	public void addSubmission(SubmissionEntry s)
	{
		super.addSubmission(s);
		if (s.isPending()) { }
		else if ("CE".equalsIgnoreCase(s.getJudgement())) { }
		else
		{
			if (s.getScore() > maxScore)
			{
				maxScore = s.getScore();
				maxScoreFirstDate = s.getSubmissionTime();
				maxScoreFirstTime = s.getContestTime();
				maxScoreId = s.getId();
			}
		}
	}
}
